package vip.meeet.algorithm.str;



public class Num14 {

    public static void main(String[] args) {
        String[] params = {"flower","flow","flight"};
        String[] params2 = {"dog","racecar","car"};
        System.out.println(longestCommonPrefix(params));
        System.out.println(longestCommonPrefix(params2));

    }

    public static String longestCommonPrefix(String[] strs) {
        if(strs.length == 0){
            return "";
        }
        if(strs.length == 1){
            return strs[0];
        }
        int minLen = strs[0].length();
        String min = strs[0];
        for(int i=1;i<strs.length;i++){
            if(strs[i].length() < minLen){
                minLen = strs[i].length();
                min = strs[i];
            }
        }
        StringBuilder sb = new StringBuilder();
        for(int i=0;i<minLen;i++){
            char currentChar = min.charAt(i);
            for(String str : strs){
                if(str.charAt(i) != currentChar){
                    return sb.toString();
                }
            }
            sb.append(currentChar);
        }
        return sb.toString();
    }
}
